package com.magicborrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.beans.NormalBean;
import com.magicborrow.beans.NormalBean1;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.LoadingDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/11.
 */
public class ElseRegistersActivity extends BaseActivity implements View.OnClickListener{

    private LinearLayout ll_frame;
    private TextView tv_confirm;
    private ImageView iv_back;
    private TextView tv_send;
    private EditText edit_phone;
    private EditText edit_code;
    private EditText edit_password;
    private String phone;
    private String checkCode;
    private String password;
    private LoadingDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registers);
        initView();
        initListener();
        initDate();
    }

    private void initDate() {


    }

    private void initView(){
        //如果验证码错误需要修改边框颜色就修改这个LinearLayout的背景颜色
        ll_frame = (LinearLayout)findViewById(R.id.ll_frame);
        tv_confirm =(TextView)findViewById(R.id.tv_confirm);
        iv_back = (ImageView)findViewById(R.id.iv_back);
        tv_send = (TextView)findViewById(R.id.tv_send);
        edit_phone = (EditText)findViewById(R.id.edit_phone);
        edit_code = (EditText)findViewById(R.id.edit_code);
        edit_password = (EditText)findViewById(R.id.edit_password);
    }

    private void initListener(){
        tv_confirm.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        tv_send.setOnClickListener(this);
    }

    private void checkCode(){
        if(!checkPhone()){return;}
        if(!checkCheckCode()){return;}
        if(!checkPassword()){return;}
        dialog = new LoadingDialog(this);
        dialog.show();
        Map<String,String> params = new HashMap<>();
        params.put("mobile",phone);
        params.put("code",checkCode);
        params.put("type","0");
        VolleyTool.get(Constants.CHECK_CODE_URL, params, new VolleyTool.HTTPListener() {
            @Override
            public <T> void onResponse(T t, int requestCode) {
                dialog.dismiss();
                String str = t.toString();
                try {
                    JSONObject obj = new JSONObject(str);
                    int code = obj.getInt("code");
                    String message = obj.getString("message");
                    if(code == 0){
                        Intent intent = new Intent();
                        intent.putExtra("phone",phone);
                        intent.putExtra("checkCode",obj.getString("data"));
                        intent.putExtra("password",password);
                        intent.setClass(ElseRegistersActivity.this,ElseRegisterActivity.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(ElseRegistersActivity.this,message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                }
            }
            @Override
            public void onErrorResponse(VolleyError error, int requestCode) {
                dialog.dismiss();
                Toast.makeText(ElseRegistersActivity.this,"服务器忙,请稍微再试",Toast.LENGTH_SHORT).show();
            }
        },0,null);


    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.tv_confirm:
                checkCode();
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_send:
                if(!checkPhone()){return;}
                Map<String,String> params = new HashMap<>();
                params.put("mobile",phone);
                params.put("type","0");
                params.put("channel","1");
                VolleyTool.get(Constants.GET_CHECK_CODE, params, new VolleyTool.HTTPListener() {
                    @Override
                    public <T> void onResponse(T t, int requestCode) {
                        NormalBean1 bean = (NormalBean1)t;
                        if(bean.getCode() == 0){
                            Toast.makeText(ElseRegistersActivity.this,"短信已发送到您的手机,请查收",Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(ElseRegistersActivity.this,bean.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error, int requestCode) {
                        Toast.makeText(ElseRegistersActivity.this,"服务器忙,请稍后重试",Toast.LENGTH_SHORT).show();
                    }
                }, 1, NormalBean1.class);

                break;
        }
    }

    private boolean checkPhone(){
        phone = edit_phone.getText().toString();
        if("".equals(phone)){
            Toast.makeText(this,"手机号不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(phone.length() < 11){
            Toast.makeText(this,"请正确填写手机号码",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private boolean checkCheckCode(){
        checkCode = edit_code.getText().toString();
        if("".equals(checkCode)){
            Toast.makeText(this,"验证码不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
    private boolean checkPassword(){
        password = edit_password.getText().toString();
        if("".equals(password)){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(password.length() < 6){
            Toast.makeText(this,"密码不能低于6位数",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

}
